package com.yudaxian.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yudaxian.common.HoneyLogs;
import com.yudaxian.common.enums.LogType;
import com.yudaxian.common.vo.Result;
import com.yudaxian.sys.entity.Item;
import com.yudaxian.sys.entity.req.ItemReq;
import com.yudaxian.sys.service.IItemService;
import com.yudaxian.sys.service.impl.ItemServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yudaxian
 * @since 2023-12-05
 */
@RestController
@RequestMapping("/item")
public class ItemController {


    @Resource
    private ItemServiceImpl itemService;

    @GetMapping("/all")
    public Result<List<Item>> getAllItems() {
        List<Item> items = itemService.list();
        return Result.success(items);
    }

    @GetMapping("/list")
    public Result<Map<String, Object>> getItemList(
            @RequestParam(value = "itemName", required = false) String itemName,
            @RequestParam(value = "pageNo") Integer pageNo,
            @RequestParam(value = "pageSize") Integer pageSize) {

        LambdaQueryWrapper<Item> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(itemName), Item::getItemName, itemName);

        Page<Item> page = new Page<>(pageNo, pageSize);
        itemService.page(page, wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("totalPage", page.getTotal());
        data.put("rows", page.getRecords());

        return Result.success(data);
    }

    @HoneyLogs(operation = "项目", type = LogType.ADD)
    @PostMapping("/add")
    //RequestBody，json转Item对象，前端输入数据，传给后台json，再转成对象，存入数据库
    public Result<?> addItem(@RequestBody ItemReq req) {
        Item item = new Item();
        BeanUtils.copyProperties(req, item);
        String jsonString = req.getItemAddress().toJSONString();
        String[] itemAddressArray = jsonString.substring(1, jsonString.length() - 1).split("\",\"");
        StringBuffer sb = new StringBuffer();
        for (String i : itemAddressArray) {
            sb.append(i).append("/");
        }
        item.setItemAddress(sb.toString());
        itemService.addItem(item);
        return Result.success("新增项目成功");
    }

    @HoneyLogs(operation = "项目", type = LogType.UPDATE)
    @PutMapping("/update")
    public Result<?> updateItem(@RequestBody ItemReq req) {
        Item item = new Item();
        BeanUtils.copyProperties(req, item);
        String jsonString = req.getItemAddress().toJSONString();
        jsonString = jsonString.replace("[", ""); // 删除左括号
        jsonString = jsonString.replace("]", ""); // 删除右括号
        jsonString = jsonString.replace("\"", ""); // 删除单引号
        jsonString = jsonString.replace(",", "--"); // 删除空格
        item.setItemAddress(jsonString);
        itemService.updateItem(item);
        return Result.success("修改成功");
    }

    @GetMapping("{id}")
    public Result<Item> getItemById(@PathVariable("id") Integer id) {
        Item item = itemService.getItemById(id);

        return Result.success(item);
    }

    @HoneyLogs(operation = "项目", type = LogType.DELETE)
    @DeleteMapping("/delete/{id}")
    public Result<?> deleteItemById(@PathVariable("id") Integer id) {
        itemService.deleteItemById(id);

        return Result.success("删除成功");
    }


}
